<?php defined('SYSPATH') or die('No direct script access.');

class Votes_Model extends Model {

	public function __construct() {		
		$this->db = Database::instance();
	}

	public function is_song_voted($profile_id, $album_id, $track_no) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_songs
					WHERE (profile_id=?)
					AND (album_id=?)
					AND (track_no=?)
					AND (DATE(created)=DATE(CURRENT_TIMESTAMP()))
			';
			$query = $this->db->query($sql, array($profile_id, $album_id, $track_no));
			if ($query->current()->voted>0) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return false;
		}	
	}

	public function is_song_limited($profile_id) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_songs
					WHERE (profile_id=?)
					AND (DATE(created)=DATE(CURRENT_TIMESTAMP()))
			';
			$query = $this->db->query($sql, array($profile_id));
			if ($query->current()->voted>=10) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return true;
		}	
	}
	
	public function vote_song($profile_id, $album_id, $track_no) {
		$this->db->query('BEGIN');
		try {
			$sql = '
					INSERT INTO profiles_vote_songs(profile_id, album_id, track_no, rating, created)
					VALUES(?, ?, ?, 1, CURRENT_TIMESTAMP());
			';
			
			$query = $this->db->query($sql, array($profile_id, $album_id, $track_no));
			$this->db->query('COMMIT');
			return true;
		}	
		catch (Exception $e) {
			$this->db->query('ROLLBACK');
			Kohana::log('error', $e);
			return false;
		}	
	}
	
	public function get_total_votes($album_id, $track_no) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_songs
					WHERE (album_id=?)
					AND (track_no=?)
			';
			$query = $this->db->query($sql, array($album_id, $track_no));
			return $query->current()->voted;
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return 0;
		}	
	}
	
	public function get_song_title($album_id, $track_no) {
		try {
			$sql = '
					SELECT title
					FROM songs
					WHERE album_id=? 
					AND track_no=?
					';
			$query = $this->db->query($sql, array($album_id, $track_no));
			return $query->current()->title;
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return "";
		}
	}
	
	public function is_channel_voted($owner_id, $profile_id, $channel_id) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_channels
					WHERE (owner_id=?)
					AND (profile_id=?)
					AND (channel_id=?)
					AND (DATE(created)=DATE(CURRENT_TIMESTAMP()))
			';
			$query = $this->db->query($sql, array($owner_id, $profile_id, $channel_id));
			if ($query->current()->voted>0) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return false;
		}	
	}
	
	public function is_channel_limited($profile_id) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_channels
					WHERE (profile_id=?)
					AND (DATE(created)=DATE(CURRENT_TIMESTAMP()))
			';
			$query = $this->db->query($sql, array($profile_id));
			if ($query->current()->voted>=10) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return true;
		}	
	}
	
	public function vote_channel($owner_id, $profile_id, $channel_id) {
		$this->db->query('BEGIN');
		try {
			$sql = '
					INSERT INTO profiles_vote_channels(owner_id, profile_id, channel_id, rating, created)
					VALUES(?, ?, ?, 1, CURRENT_TIMESTAMP());
			';
			
			$query = $this->db->query($sql, array($owner_id, $profile_id, $channel_id));
			$this->db->query('COMMIT');
			return true;
		}	
		catch (Exception $e) {
			$this->db->query('ROLLBACK');
			Kohana::log('error', $e);
			return false;
		}	
	}
	
	public function get_total_channel_votes($owner_id, $channel_id) {
		try {
			$sql = '
					SELECT count(*) as voted
					FROM profiles_vote_channels
					WHERE (owner_id=?)
					AND (channel_id=?)
			';
			$query = $this->db->query($sql, array($owner_id, $channel_id));
			return $query->current()->voted;
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return 0;
		}	
	}
	
	public function get_channel_title($owner_id, $channel_id) {
		try {
			$sql = '
					SELECT title
					FROM channels
					WHERE owner_id=? 
					AND id=?
					';
			$query = $this->db->query($sql, array($owner_id, $channel_id));
			return $query->current()->title;
		}
		catch (Exception $e) {
			Kohana::log('error', $e);
			return "";
		}
	}
	
	public function download_file_mp3($profile_id, $album_id, $track_no) {
		$this->db->query('BEGIN');
		try {
			$sql = '
				SELECT *
				FROM songs
				WHERE album_id=? AND track_no=?	
			';	
			$result = $this->db->query($sql, array($album_id, $track_no))->result(FALSE);
			
			$auth = new Auth();
			$uri_mp3 = '';
			foreach($result as $row) {
				if ($auth->logged_in('admin')) {
					$uri_mp3 = url::base() . rawurlencode($row['high_bit_rate_file']);
				}
				else if ($auth->logged_in('vip')) {
					$uri_mp3 = url::base() . rawurlencode($row['high_bit_rate_file']);
				}
				else if ($auth->logged_in('member')) {
					$uri_mp3 = url::base() . rawurlencode($row['low_bit_rate_file']);
				}
				else {
					$uri_mp3 = url::base() . rawurlencode($row['sample_file']);
				}
				$uri_mp3 = str_replace('%2F','/',$uri_mp3);
			}
			
			$sql = '
					INSERT INTO profiles_download_songs(profile_id, album_id, track_no, created)
					VALUES(?, ?, ?, CURRENT_TIMESTAMP());
			';
			
			$query = $this->db->query($sql, array($profile_id, $album_id, $track_no));
			$this->db->query('COMMIT');
			
			return $uri_mp3;
		}
		catch (Exception $e) {
			$this->db->query('ROLLBACK');
			Kohana::log('error', $e);
			return '';
		}
	} 
}